/* rcshwbc3.p - SHIPPER WORKBENCH SUBPROGRAM - ADD KIT COMPONENTS             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.8.1.7 $                                                       */
/*V8:ConvertMode=NoConvert                                                    */
/* REVISION: 8.6      LAST MODIFIED: 10/14/96       BY: kxn *K003*            */
/* REVISION: 8.6      LAST MODIFIED: 11/22/96       BY: kxn *K022*            */
/* REVISION: 8.6      LAST MODIFIED: 06/10/97       BY: *K0DY* Taek-Soo Chang */
/* REVISION: 8.6      LAST MODIFIED: 07/10/97       BY: *K0FH* Taek-Soo Chang */
/* REVISION: 8.6      LAST MODIFIED: 11/11/97       BY: *K18W* Suresh Nayak   */

/* REVISION: 8.6E     LAST MODIFIED: 05/20/98       BY: *K1Q4* Alfred Tan     */
/* REVISION: 8.6E     LAST MODIFIED: 06/01/98       BY: *K1NF* Niranjan R.    */
/* REVISION: 8.6E     LAST MODIFIED: 07/22/98       BY: *J2M7* Niranjan R.    */
/* REVISION: 8.6E     LAST MODIFIED: 07/19/99       BY: *K21J* Poonam Bahl    */
/* REVISION: 9.1      LAST MODIFIED: 11/23/99       BY: *K23D* Reetu Kapoor   */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00       BY: *N0KP* myb            */
/* REVISION: 9.1      LAST MODIFIED: 12/28/00       BY: *L13Y* Kaustubh K.    */
/* Revision: 1.8.1.5     BY: Kirti Desai          DATE: 06/18/01 ECO: *M1B8*  */
/* $Revision: 1.8.1.7 $   BY:  Mercy Chittilapilly DATE: 08/27/02 ECO: *N1SJ* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}

/* INPUT VARIABLES */
define input parameter line_recno   as   recid        no-undo.
define input parameter sod_recno    as   recid        no-undo.
define input parameter qty_ratio    as   decimal      no-undo.
define input parameter this_rec_qty as   decimal      no-undo.
define input parameter site         like lad_site     no-undo.
define input parameter loc          like lad_loc      no-undo.
define input parameter lot          like lad_lot      no-undo.
define input parameter ref          like lad_ref      no-undo.
define input parameter det_all      as   logical      no-undo.

/* LOCAL VARIABLES */
define variable abs_recno           as   recid        no-undo.
define variable so_recno            as   recid        no-undo.
define variable comp_recno          as   recid        no-undo.
define variable par_absid           like abs_id       no-undo.
define variable par_shipfrom        like abs_shipfrom no-undo.
define variable trans_conv          as   decimal      no-undo.
define variable qty_lft             as   decimal      no-undo.
define variable orderid             like abs_id       no-undo.
define variable ordernbr            like abs_order    no-undo.
define variable orderlin            like abs_line     no-undo.
define variable l_abs_tare_wt       like abs_nwt      no-undo.

define shared temp-table work_sr_wkfl like sr_wkfl.

/* BUFFERS */
define buffer ship_line for abs_mstr.
define buffer check_abs for abs_mstr.

{rcinvtbl.i new}
{soabsdef.i}

for first sod_det
   fields (sod_cfg_type sod_fa_nbr sod_line sod_nbr sod_part)
   where recid(sod_det) = sod_recno
   no-lock:

end. /* FOR FIRST sod_det */


for first so_mstr
   fields(so_nbr)
   where so_nbr = sod_nbr
   no-lock:

end. /* FOR FIRST so_mstr */

for first check_abs
   fields (abs_dataset abs_fa_lot abs_gwt abs_id abs_item
           abs_line abs_loc abs_lotser abs_nwt abs_order
           abs_par_id abs_qty abs_ref abs_shipfrom abs_shipto
           abs_site abs_type abs_wt_um abs__qad02
           abs__qad03 abs__qad10)
   where recid(check_abs) = line_recno
   no-lock:

end. /* FOR FIRST check_abs */

/* SAVE THE ORDER NUMBER AND LINE OF PARENT ITEM */

assign
   qty_lft  = this_rec_qty
   orderid  = check_abs.abs_id
   ordernbr = check_abs.abs_order
   orderlin = check_abs.abs_line.

run get_abs_parent (input check_abs.abs_id,
                    input check_abs.abs_shipfrom,
                    output par_absid,
                    output par_shipfrom).

for first abs_mstr
   fields (abs_dataset abs_fa_lot abs_gwt abs_id abs_item
           abs_line abs_loc abs_lotser abs_nwt abs_order
           abs_par_id abs_qty abs_ref abs_shipfrom abs_shipto
           abs_site abs_type abs_wt_um abs__qad02
           abs__qad03 abs__qad10)
   where abs_mstr.abs_shipfrom = par_shipfrom
   and   abs_mstr.abs_id       = par_absid
   no-lock:

end. /* FOR FIRST abs_mstr */

abs_recno = recid (abs_mstr).

KIT_COMP:
for each qad_wkfl exclusive-lock
   where qad_key1 = mfguser + global_db + "stage_component"
   and   qad_key2 begins sod_nbr + "," + string(sod_line) + ","
   and   qad_key3 = sod_nbr
   and   qad_key4 = string(sod_line)
   break by qad_charfld[2]:

   if first-of (qad_charfld[2])
   then
      qty_lft = this_rec_qty.

   /* IF STAGE QUANTITY THEN CREATE A RECORD */
   if qad_decfld[1] <> 0
   then do:

      for first pt_mstr
         fields (pt_lot_ser pt_net_wt pt_net_wt_um
                 pt_part    pt_ship_wt)
         where pt_part = qad_charfld[2]
         no-lock:

      end. /* FOR FIRST pt_mstr */

      /*  FOR SERIALIZED ITEM WRITE REC WITH QTY OF 1 */
      if available pt_mstr
         and pt_lot_ser = "S"
      then do:

         if qty_lft <> 0
         then do:

            create ship_line.

            /*  USE BOTH PARENT'S LOC, LOT & CHILD'S LOC, LOT IN ID      */
            /*  TO AVOID DUPLICATE RECORD IN THE CASE OF MULTI ENTRY     */
            /*  CHANGE HEADER ID TO THE ID OF KIT PARENT WHICH INCLUDE   */
            /*  EITHER SO# OR PO# & LINE, WORK WITH BOTH PO & SO SHIPPER */

            assign
               ship_line.abs_shipfrom = abs_mstr.abs_shipfrom
               ship_line.abs_id       =  orderid          /* PARENT ID */
                                         + qad_charfld[1] /* SITE      */
                                         + ordernbr       /* ORDER NUM */
                                         + orderlin       /* ORDER LIN */
                                         + qad_charfld[2] /* PART      */
                                         + qad_charfld[3] /* LOC       */
                                         + qad_charfld[4] /* LOT       */
                                         + qad_charfld[5] /* REF       */
               ship_line.abs_par_id   = abs_mstr.abs_id   /* HEADER ID */
               ship_line.abs_order    = ordernbr
               ship_line.abs_line     = orderlin
               ship_line.abs_type     = abs_mstr.abs_type
               ship_line.abs_shipto   = abs_mstr.abs_shipto
               ship_line.abs_item     = qad_charfld[2]
               ship_line.abs_site     = qad_charfld[1]
               ship_line.abs_loc      = qad_charfld[3]
               ship_line.abs_lot      = qad_charfld[4]
               ship_line.abs_ref      = qad_charfld[5]
               ship_line.abs_qty      = minimum (qty_lft,qad_decfld[1]) .

            if recid(ship_line) = -1
            then .

            if det_all
            then do:

               for first tmp_alloc_mstr
                  fields (tmp_item  tmp_line tmp_loc tmp_lot
                          tmp_order tmp_qty  tmp_ref tmp_site)
                  where tmp_order = ship_line.abs_order
                  and   tmp_line  = ship_line.abs_line
                  and   tmp_site  = ship_line.abs_site
                  and   tmp_loc   = ship_line.abs_loc
                  and   tmp_item  = ship_line.abs_item
                  and   tmp_lot   = ship_line.abs_lot
                  and   tmp_ref   = ship_line.abs_ref
                  no-lock:

               end. /* FOR FIRST tmp_alloc_mstr */

               if available tmp_alloc_mstr
                  and execname = "rcshwb.p"
               then do:
                  {abspack.i "ship_line" 3 22 "tmp_qty"}
               end. /* IF AVAILABLE tmp_alloc_mstr AND ... */

               else do:
                  {abspack.i "ship_line" 3 22 "ship_line.abs_qty" }
               end. /* ELSE DO */

            end. /* IF det_all */

            if available sod_det
            then do:

                /* STORING UOM AND UM CONVERSION FACTOR FOR CONFIGURED */
                /* ITEMS IN abs__qad02 AND abs__qad03                  */
               assign
                  ship_line.abs__qad02 = qad_charfld[10]
                  ship_line.abs__qad03 = qad_charfld[11].

               /* ASSIGN FINAL ASSEMBLY LOT NUMBER */
               if sod_cfg_type  = "1"
                  or sod_fa_nbr > ""
               then
                  ship_line.abs_fa_lot = qad_charfld[15].

               /* RE-ASSIGN PARENT ID TO THE KIT IF THIS IS A COMPONENT */
               if abs_recno   <> line_recno
                  or sod_part <> ship_line.abs_item
               then

                  for first check_abs
                     fields (abs_dataset abs_fa_lot abs_gwt abs_id abs_item
                             abs_line abs_loc abs_lotser abs_nwt abs_order
                             abs_par_id abs_qty abs_ref abs_shipfrom abs_shipto
                             abs_site abs_type abs_wt_um abs__qad02
                             abs__qad03 abs__qad10)
                     where recid(check_abs) = line_recno
                     no-lock:

                  end. /* FOR FIRST check_abs */

                  if available check_abs
                  then
                     ship_line.abs_par_id = check_abs.abs_id.

            end. /* IF AVAILABLE sod_det */

            assign
               qty_lft       = qty_lft       - ship_line.abs_qty
               qad_decfld[1] = qad_decfld[1] - ship_line.abs_qty.

         end.  /* IF qty_lft > 0 */

      end.    /* IF AVAILABLE pt_mstr AND pt_lot_ser = "S" */

      else do:

         /* NON SERIALIZED ITEMS   */
         create ship_line.

         /* FOR ORACLE ENVIRONMENT, COMMITING THE RECORD CREATION */
         /* AFTER ALL THE INDEX FIELDS HAVE BEEN ASSIGNED.        */

         assign
            ship_line.abs_shipfrom = abs_mstr.abs_shipfrom
            ship_line.abs_id       = orderid               /* PARENT ID */
                                       + qad_charfld[1]    /* SITE      */
                                       + ordernbr          /* ORDER NUM */
                                       + orderlin          /* ORDER LIN */
                                       + qad_charfld[2]    /* PART      */
                                       + qad_charfld[3]    /* LOC       */
                                       + qad_charfld[4]    /* LOT       */
                                       + qad_charfld[5]    /* REF       */
            ship_line.abs_par_id   = abs_mstr.abs_id       /* HEADER ID */
            ship_line.abs_order    = ordernbr
            ship_line.abs_line     = orderlin
            ship_line.abs_shipto   = abs_mstr.abs_shipto
            ship_line.abs_type     = abs_mstr.abs_type
            ship_line.abs_qty      = qad_decfld[1] * qty_ratio
            ship_line.abs_item     = qad_charfld[2]
            ship_line.abs_site     = qad_charfld[1]
            ship_line.abs_loc      = qad_charfld[3]
            ship_line.abs_lot      = qad_charfld[4]
            ship_line.abs_ref      = qad_charfld[5].

         if recid(ship_line) = -1
         then .

         if det_all
         then do:

            for first tmp_alloc_mstr
               fields (tmp_item  tmp_line tmp_loc tmp_lot
                       tmp_order tmp_qty  tmp_ref tmp_site)
               where tmp_order = ship_line.abs_order
               and   tmp_line  = ship_line.abs_line
               and   tmp_site  = ship_line.abs_site
               and   tmp_loc   = ship_line.abs_loc
               and   tmp_item  = ship_line.abs_item
               and   tmp_lot   = ship_line.abs_lot
               and   tmp_ref   = ship_line.abs_ref
               no-lock:

            end. /* FOR FIRST tmp_alloc_mstr */

            if available tmp_alloc_mstr
               and execname = "rcshwb.p"
            then do:
               {abspack.i "ship_line" 3 22 "tmp_qty"}
            end. /* IF AVAILABLE tmp_alloc_mstr AND ... */

            else do:
               {abspack.i "ship_line" 3 22 "ship_line.abs_qty" }
            end. /* ELSE DO */

         end. /* IF det_all */

         if available sod_det
         then do:

            /* STORING UOM AND UM CONVERSION FACTOR FOR CONFIGURED */
            /* ITEMS IN abs__qad02 AND abs__qad03                  */
            assign
               ship_line.abs__qad02 = qad_charfld[10]
               ship_line.abs__qad03 = qad_charfld[11].

            /* ASSIGN FINAL ASSEMBLY LOT NUMBER */
            if sod_cfg_type  = "1"
               or sod_fa_nbr > ""
            then
               ship_line.abs_fa_lot = qad_charfld[15].

            /* RE-ASSIGN PARENT ID TO THE KIT IF THIS IS A COMPONENT */
            if abs_recno   <> line_recno
               or sod_part <> ship_line.abs_item
            then
               for first check_abs
                  fields (abs_dataset abs_fa_lot abs_gwt abs_id abs_item
                          abs_line abs_loc abs_lotser abs_nwt abs_order
                          abs_par_id abs_qty abs_ref abs_shipfrom abs_shipto
                          abs_site abs_type abs_wt_um abs__qad02
                          abs__qad03 abs__qad10)
                  where recid(check_abs) = line_recno
                  no-lock:

               end. /* FOR FIRST check_abs */

               if available check_abs
               then
                  ship_line.abs_par_id = check_abs.abs_id.

         end. /* IF AVAILABLE sod_det */

      end. /* ELSE DO */

      if available     pt_mstr
         and available ship_line
      then do:
         assign
            ship_line.abs_nwt   = MAX(pt_net_wt * ship_line.abs_qty *
                                  decimal(abs__qad03),0)
            ship_line.abs_gwt   = if pt_ship_wt <> 0
                                  then
                                     MAX(pt_ship_wt * ship_line.abs_qty *
                                     decimal(abs__qad03),0)
                                  else
                                     MAX(pt_net_wt * ship_line.abs_qty *
                                     decimal(abs__qad03),0)
            l_abs_tare_wt       = MAX((pt_ship_wt - pt_net_wt) *
                                  ship_line.abs_qty * decimal(abs__qad03),0)
            ship_line.abs_wt_um = pt_net_wt_um.

         {abspack.i "ship_line" 26 22 "l_abs_tare_wt" }

      end. /* IF AVAILABLE pt_mstr AND AVAILABLE ship_line */

   end. /* IF qad_decfld[1]  <> 0 */

end. /* FOR EACH qad_wkfl */

{rcinvcon.i}
